﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System.IO;
using System.Collections;

namespace 边界搜索
{
    public partial class Form1 : Form
    {
        AcadApplication AcadApp;
        AcadDocument AcadDoc;

        private ArrayList arrayEdges = new ArrayList();

        public Form1()
        {
            InitializeComponent();
        }

        private void 启动CAD()
        {
            try
            {
                AcadApp =
               (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
                AcadDoc = AcadApp.ActiveDocument;
            }
            catch
            {
                OpenFileDialog pOpenDG = new OpenFileDialog();
                pOpenDG.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf";
                pOpenDG.Title = "打开CAD文件";
                pOpenDG.ShowDialog();
                string filePath = pOpenDG.FileName;
                if (filePath == "")
                {
                    MessageBox.Show("选择CAD文件无效！", "文件无效！");
                    Application.Exit();
                }
                AcadApp = new AcadApplication();
                AcadDoc = AcadApp.Documents.Open(filePath, null, null);
            }
            AcadApp.Application.Visible = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            启动CAD();
        }

        private void 凸边界搜索_Click(object sender, EventArgs e)
        {
            arrayEdges.Clear();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet001");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "POINT";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] arrPoints = new double[3 * mySelectionSet.Count];
            int count = 0;
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbPoint")
                {
                    count++;
                    double[] PointCoord;
                    PointCoord = (Double[])(((AcadPoint)acadObj).Coordinates);
                    arrPoints[3 * count - 3] = PointCoord[0];
                    arrPoints[3 * count - 2] = PointCoord[1];
                    arrPoints[3 * count - 1] = PointCoord[2];
                }
            }
            AcadDoc.SelectionSets.Item("NewSelectionSet001").Delete();
            //获取点集外围边界 
            int i, StartIndex = 0, AIndex, BIndex, pointCount = arrPoints.Length / 3;
            for (i = 1; i < pointCount; i++) //寻找X值最小的点号
            {
                if (arrPoints[3 * i] < arrPoints[3 * StartIndex])
                {
                    StartIndex = i;
                }
            }
            Edge edge = new Edge();
            edge.Start = StartIndex;
            BIndex = StartIndex - 1;
            AIndex = StartIndex;
            double[] vector1 = new double[2], vector2 = new double[2];
            vector1[0] = 0; vector1[1] = 100;
            double vector1Length, vector2Length, angleTemp, angleMax, lengthMin, vectorDirect;
            angleMax = 0;
            while (BIndex != StartIndex)
            {
                vector1Length = Math.Sqrt(vector1[0] * vector1[0] + vector1[1] * vector1[1]);
                lengthMin = 300;
                for (i = 0; i < pointCount; i++)//找边界
                {
                    if (i != edge.Start)
                    {
                        vector2[0] = arrPoints[3 * i] - arrPoints[3 * AIndex];
                        vector2[1] = arrPoints[3 * i + 1] - arrPoints[3 * AIndex + 1];
                        vector2Length = Math.Sqrt(vector2[0] * vector2[0] + vector2[1] * vector2[1]);
                        angleTemp = Math.Acos((vector1[0] * vector2[0] + vector1[1] * vector2[1]) /
                       (vector1Length * vector2Length));
                        vectorDirect = vector1[0] * vector2[1] - vector1[1] * vector2[0];
                        if (angleTemp > angleMax && vectorDirect < 0)
                        {
                            angleMax = angleTemp;
                            edge.End = i;
                            lengthMin = vector2Length;
                        }
                        else if (angleTemp == angleMax && vector2Length < lengthMin)
                        {
                            edge.End = i;
                            lengthMin = vector2Length;
                        }
                    }
                }
                arrayEdges.Add(edge);
                BIndex = edge.End;
                edge = new Edge();
                edge.Start = BIndex;

                vector1[0] = arrPoints[3 * AIndex] - arrPoints[3 * BIndex];
                vector1[1] = arrPoints[3 * AIndex + 1] - arrPoints[3 * BIndex + 1];
                angleMax = 0;
                AIndex = BIndex;
            }
            //绘制边界
            double[] lineCoords = new double[3 * (arrayEdges.Count + 1)];
            Edge[] edges = new Edge[arrayEdges.Count];
            arrayEdges.CopyTo(edges);
            for (i = 0; i < arrayEdges.Count; i++)
            {
                double[] p1 = new double[3];
                p1[0] = arrPoints[3 * edges[i].Start];
                p1[1] = arrPoints[3 * edges[i].Start + 1];
                p1[2] = arrPoints[3 * edges[i].Start + 2];
                lineCoords[3 * i] = arrPoints[3 * edges[i].Start];
                lineCoords[3 * i + 1] = arrPoints[3 * edges[i].Start + 1];
                lineCoords[3 * i + 2] = arrPoints[3 * edges[i].Start + 2];
            }
            lineCoords[3 * arrayEdges.Count - 3] = arrPoints[3 * edges[arrayEdges.Count - 1].Start];
            lineCoords[3 * arrayEdges.Count - 2] = arrPoints[3 * edges[arrayEdges.Count - 1].Start
           + 1];
            lineCoords[3 * arrayEdges.Count - 1] = arrPoints[3 * edges[arrayEdges.Count - 1].Start
           + 2];
            lineCoords[3 * arrayEdges.Count] = arrPoints[3 * edges[arrayEdges.Count - 1].End];
            lineCoords[3 * arrayEdges.Count + 1] = arrPoints[3 * edges[arrayEdges.Count - 1].End + 1];
            lineCoords[3 * arrayEdges.Count + 2] = arrPoints[3 * edges[arrayEdges.Count - 1].End + 2];
            AcadDoc.ModelSpace.Add3DPoly(lineCoords);
        }

        private void 含凹边界的凸边界搜索_Click(object sender, EventArgs e)
        {
            arrayEdges.Clear();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet001");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "POINT";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] arrPoints = new double[3 * mySelectionSet.Count];
            int count = 0;
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbPoint")
                {
                    count++;
                    double[] PointCoord;
                    PointCoord = (Double[])(((AcadPoint)acadObj).Coordinates);
                    arrPoints[3 * count - 3] = PointCoord[0];
                    arrPoints[3 * count - 2] = PointCoord[1];
                    arrPoints[3 * count - 1] = PointCoord[2];
                }
            }
            AcadDoc.SelectionSets.Item("NewSelectionSet001").Delete();
            //获取点集外围边界 
            int i, StartIndex = 0, AIndex, BIndex, pointCount = arrPoints.Length / 3;
            for (i = 1; i < pointCount; i++) //寻找X值最小的点号
            {
                if (arrPoints[3 * i] < arrPoints[3 * StartIndex])
                {
                    StartIndex = i;
                }
            }
            Edge edge = new Edge();
            edge.Start = StartIndex;
            BIndex = StartIndex - 1;
            AIndex = StartIndex;
            double[] vector1 = new double[2], vector2 = new double[2];
            vector1[0] = 0; vector1[1] = 100;
            double vector1Length, vector2Length, angleTemp, angleMax, lengthMin, vectorDirect;
            angleMax = 0;
            while (BIndex != StartIndex)
            {
                vector1Length = Math.Sqrt(vector1[0] * vector1[0] + vector1[1] * vector1[1]);
                lengthMin = 300;
                for (i = 0; i < pointCount; i++)//找边界
                {
                    if (i != edge.Start)
                    {
                        vector2[0] = arrPoints[3 * i] - arrPoints[3 * AIndex];
                        vector2[1] = arrPoints[3 * i + 1] - arrPoints[3 * AIndex + 1];
                        vector2Length = Math.Sqrt(vector2[0] * vector2[0] + vector2[1] * vector2[1]);
                        angleTemp = Math.Acos((vector1[0] * vector2[0] + vector1[1] * vector2[1]) /
                       (vector1Length * vector2Length));
                        vectorDirect = vector1[0] * vector2[1] - vector1[1] * vector2[0];
                        if (angleTemp > angleMax && vectorDirect < 0)
                        {
                            angleMax = angleTemp;
                            edge.End = i;
                            lengthMin = vector2Length;
                        }
                        else if (angleTemp == angleMax && vector2Length < lengthMin)
                        {
                            edge.End = i;
                            lengthMin = vector2Length;
                        }
                    }
                }
                arrayEdges.Add(edge);
                BIndex = edge.End;
                edge = new Edge();
                edge.Start = BIndex;

                vector1[0] = arrPoints[3 * AIndex] - arrPoints[3 * BIndex];
                vector1[1] = arrPoints[3 * AIndex + 1] - arrPoints[3 * BIndex + 1];
                angleMax = 0;
                AIndex = BIndex;
            }
            //绘制边界
            double[] lineCoords = new double[3 * (arrayEdges.Count + 1)];
            Edge[] edges = new Edge[arrayEdges.Count];
            arrayEdges.CopyTo(edges);
            for (i = 0; i < arrayEdges.Count; i++)
            {
                double[] p1 = new double[3];
                p1[0] = arrPoints[3 * edges[i].Start];
                p1[1] = arrPoints[3 * edges[i].Start + 1];
                p1[2] = arrPoints[3 * edges[i].Start + 2];
                lineCoords[3 * i] = arrPoints[3 * edges[i].Start];
                lineCoords[3 * i + 1] = arrPoints[3 * edges[i].Start + 1];
                lineCoords[3 * i + 2] = arrPoints[3 * edges[i].Start + 2];
            }
            lineCoords[3 * arrayEdges.Count - 3] = arrPoints[3 * edges[arrayEdges.Count - 1].Start];
            lineCoords[3 * arrayEdges.Count - 2] = arrPoints[3 * edges[arrayEdges.Count - 1].Start
           + 1];
            lineCoords[3 * arrayEdges.Count - 1] = arrPoints[3 * edges[arrayEdges.Count - 1].Start
           + 2];
            lineCoords[3 * arrayEdges.Count] = arrPoints[3 * edges[arrayEdges.Count - 1].End];
            lineCoords[3 * arrayEdges.Count + 1] = arrPoints[3 * edges[arrayEdges.Count - 1].End + 1];
            lineCoords[3 * arrayEdges.Count + 2] = arrPoints[3 * edges[arrayEdges.Count - 1].End + 2];
            AcadSelectionSet mySelectionSetTemp;
            mySelectionSetTemp = AcadDoc.SelectionSets.Add("NewSelectionSet003");
            Int16[] FilterType1 = new Int16[1];
            object[] FilterData1 = new object[1];
            FilterType1[0] = 0;
            FilterData1[0] = "POINT";
            mySelectionSetTemp.SelectByPolygon(AcSelect.acSelectionSetWindowPolygon, lineCoords,
           FilterType1, FilterData1);
            if (mySelectionSetTemp.Count > 0) //若选择集中的选择对象个数大于0则表示存在凹边界
            {
                double newAngleTemp, newMaxAngle = 0, newVector1Length, newVector2Length;
                double[] newVector01 = new double[2], newVector02 = new double[2];
                int insertIndex = 0, pointArrayIndex = 0;
                Edge[] newEdges;
                foreach (AcadObject acadObj in mySelectionSetTemp)
                {
                    if (acadObj.ObjectName == "AcDbPoint")
                    {
                        newEdges = new Edge[arrayEdges.Count];
                        arrayEdges.CopyTo(newEdges);
                        double[] pointCoords = new double[3];
                        pointCoords = (double[])((AcadPoint)acadObj).Coordinates;
                        for (i = 0; i < edges.Length; i++)
                        {
                            newVector01[0] = arrPoints[3 * newEdges[i].Start] - pointCoords[0];
                            newVector01[1] = arrPoints[3 * newEdges[i].Start + 1] - pointCoords[1];
                            newVector02[0] = arrPoints[3 * newEdges[i].End] - pointCoords[0];
                            newVector02[1] = arrPoints[3 * newEdges[i].End + 1] - pointCoords[1];
                            newVector1Length = Math.Sqrt(newVector01[0] * newVector01[0] +
                           newVector01[1] * newVector01[1]);
                            newVector2Length = Math.Sqrt(newVector02[0] * newVector02[0] +
                           newVector02[1] * newVector02[1]);
                            newAngleTemp = Math.Acos((newVector01[0] * newVector02[0] + newVector01[1]
                           * newVector02[1]) / (newVector1Length * newVector2Length));
                            if (newAngleTemp > newMaxAngle)
                            {
                                newMaxAngle = newAngleTemp;
                                insertIndex = i;
                            }
                        }
                        //选择集中点在arrPoints数组中对应的位置
                        for (i = 0; i < arrPoints.Length / 3; i++)
                        {
                            if (pointCoords[0] == arrPoints[3 * i])
                            {
                                if (pointCoords[1] == arrPoints[3 * i + 1])
                                {
                                    if (pointCoords[2] == arrPoints[3 * i + 2])
                                    {
                                        pointArrayIndex = i;
                                        break;
                                    }
                                }
                            }
                        }
                        //插入新的边界
                        Edge newEdge01 = new Edge();
                        Edge newEdge02 = new Edge();
                        newEdge01.Start = newEdges[insertIndex].Start;
                        newEdge01.End = pointArrayIndex;
                        newEdge02.Start = pointArrayIndex;
                        newEdge02.End = newEdges[insertIndex].End;
                        arrayEdges.RemoveAt(insertIndex);
                        arrayEdges.Insert(insertIndex, newEdge01);
                        arrayEdges.Insert(insertIndex + 1, newEdge02);
                        newMaxAngle = 0;
                    }
                }
            }
            AcadDoc.SelectionSets.Item("NewSelectionSet003").Delete();
            //绘制新的边界
            double[] newLineCoords = new double[3 * (arrayEdges.Count + 1)];
            Edge[] lastEdges = new Edge[arrayEdges.Count];
            arrayEdges.CopyTo(lastEdges);
            for (i = 0; i < arrayEdges.Count; i++)
            {
                double[] p1 = new double[3], p2 = new double[3];
                p1[0] = arrPoints[3 * lastEdges[i].Start];
                p1[1] = arrPoints[3 * lastEdges[i].Start + 1];
                p1[2] = arrPoints[3 * lastEdges[i].Start + 2];
                p2[0] = arrPoints[3 * lastEdges[i].End];
                p2[1] = arrPoints[3 * lastEdges[i].End + 1];
                p2[2] = arrPoints[3 * lastEdges[i].End + 2];
                newLineCoords[3 * i] = arrPoints[3 * lastEdges[i].Start];
                newLineCoords[3 * i + 1] = arrPoints[3 * lastEdges[i].Start + 1];
                newLineCoords[3 * i + 2] = arrPoints[3 * lastEdges[i].Start + 2];
            }
            newLineCoords[3 * arrayEdges.Count - 3] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].Start];
            newLineCoords[3 * arrayEdges.Count - 2] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].Start + 1];
            newLineCoords[3 * arrayEdges.Count - 1] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].Start + 2];
            newLineCoords[3 * arrayEdges.Count] = arrPoints[3 * lastEdges[arrayEdges.Count - 1].End];
            newLineCoords[3 * arrayEdges.Count + 1] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].End + 1];
            newLineCoords[3 * arrayEdges.Count + 2] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].End + 2];
            AcadDoc.ModelSpace.Add3DPoly(newLineCoords);
        }

        private bool JudgeIsBorderVertex(int pIndex)//判断该点是否为边界点，若是则返回true 
        {
            Edge[] edges = new Edge[arrayEdges.Count];
            arrayEdges.CopyTo(edges);
            for (int i = 0; i < edges.Length; i++)
            {
                if (pIndex == edges[i].Start)
                {
                    return true;
                }
            }
            return false;
        }

        private bool JudgeInBorder(int pIndex, double[] allPoints)
        {
            Edge[] edges = new Edge[arrayEdges.Count];
            arrayEdges.CopyTo(edges);
            double[] vector01 = new double[2], vector02 = new double[2];
            for (int i = 0; i < edges.Length - 1; i++)
            {
                vector01[0] = allPoints[3 * edges[i].End] - allPoints[3 * edges[i].Start];
                vector01[1] = allPoints[3 * edges[i].End + 1] - allPoints[3 * edges[i].Start + 1];
                vector02[0] = allPoints[3 * pIndex] - allPoints[3 * edges[i].Start];
                vector02[1] = allPoints[3 * pIndex + 1] - allPoints[3 * edges[i].Start + 1];
                if (vector01[0] * vector02[1] - vector01[1] * vector02[0] < 0)
                {
                    return false;
                }
            }
            return true;
        }

        private void 非选择集方式搜索边界_Click(object sender, EventArgs e)
        {
            arrayEdges.Clear();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet004");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "POINT";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double[] arrPoints = new double[3 * mySelectionSet.Count];
            int count = 0;
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbPoint")
                {
                    count++;
                    double[] PointCoord;
                    PointCoord = (Double[])(((AcadPoint)acadObj).Coordinates);
                    arrPoints[3 * count - 3] = PointCoord[0];
                    arrPoints[3 * count - 2] = PointCoord[1];
                    arrPoints[3 * count - 1] = PointCoord[2];
                }
            }
            AcadDoc.SelectionSets.Item("NewSelectionSet004").Delete();
            //获取点集外围边界 
            int i, StartIndex = 0, AIndex, BIndex, pointCount = arrPoints.Length / 3;
            for (i = 1; i < pointCount; i++) //寻找X值最小的点号
            {
                if (arrPoints[3 * i] < arrPoints[3 * StartIndex])
                {
                    StartIndex = i;
                }
            }
            Edge edge = new Edge();
            edge.Start = StartIndex;
            BIndex = StartIndex - 1;
            AIndex = StartIndex;
            double[] vector1 = new double[2], vector2 = new double[2];
            vector1[0] = 0; vector1[1] = 100;
            double vector1Length, vector2Length, angleTemp, angleMax, lengthMin, vectorDirect;
            angleMax = 0;
            while (BIndex != StartIndex)
            {
                vector1Length = Math.Sqrt(vector1[0] * vector1[0] + vector1[1] * vector1[1]);
                lengthMin = 300;
                for (i = 0; i < pointCount; i++)//找边界
                {
                    if (i != edge.Start)
                    {
                        vector2[0] = arrPoints[3 * i] - arrPoints[3 * AIndex];
                        vector2[1] = arrPoints[3 * i + 1] - arrPoints[3 * AIndex + 1];
                        vector2Length = Math.Sqrt(vector2[0] * vector2[0] + vector2[1] * vector2[1]);
                        angleTemp = Math.Acos((vector1[0] * vector2[0] + vector1[1] * vector2[1]) /
(vector1Length * vector2Length));
                        vectorDirect = vector1[0] * vector2[1] - vector1[1] * vector2[0];
                        if (angleTemp > angleMax && vectorDirect < 0)
                        {
                            angleMax = angleTemp;
                            edge.End = i;
                            lengthMin = vector2Length;
                        }
                        else if (angleTemp == angleMax && vector2Length < lengthMin)
                        {
                            edge.End = i;
                            lengthMin = vector2Length;
                        }
                    }
                }
                arrayEdges.Add(edge);
                BIndex = edge.End;
                edge = new Edge();
                edge.Start = BIndex;
                vector1[0] = arrPoints[3 * AIndex] - arrPoints[3 * BIndex];
                vector1[1] = arrPoints[3 * AIndex + 1] - arrPoints[3 * BIndex + 1];
                angleMax = 0;
                AIndex = BIndex;
            }
            double newAngleTemp, newMaxAngle = 0, newVector1Length, newVector2Length;
            double[] newVector01 = new double[2], newVector02 = new double[2];
            int insertIndex = 0;
            Edge[] newEdges;
            for (i = 0; i < arrPoints.Length / 3; i++)
            {
                if (!JudgeIsBorderVertex(i))//若该点不为边界点时
                {
                    if (JudgeInBorder(i, arrPoints))
                    {
                        newEdges = new Edge[arrayEdges.Count];
                        arrayEdges.CopyTo(newEdges);
                        double[] pointCoords = new double[3];
                        pointCoords[0] = arrPoints[3 * i];
                        pointCoords[1] = arrPoints[3 * i + 1];
                        pointCoords[2] = arrPoints[3 * i + 2];
                        for (int j = 0; j < newEdges.Length; j++)
                        {
                            newVector01[0] = arrPoints[3 * newEdges[j].Start] - pointCoords[0];
                            newVector01[1] = arrPoints[3 * newEdges[j].Start + 1] - pointCoords[1];
                            newVector02[0] = arrPoints[3 * newEdges[j].End] - pointCoords[0];
                            newVector02[1] = arrPoints[3 * newEdges[j].End + 1] - pointCoords[1];
                            newVector1Length = Math.Sqrt(newVector01[0] * newVector01[0] +
                           newVector01[1] * newVector01[1]);
                            newVector2Length = Math.Sqrt(newVector02[0] * newVector02[0] +
                           newVector02[1] * newVector02[1]);
                            newAngleTemp = Math.Acos((newVector01[0] * newVector02[0] +
                           newVector01[1] * newVector02[1]) / (newVector1Length * newVector2Length));
                            if (newAngleTemp > newMaxAngle)
                            {
                                newMaxAngle = newAngleTemp;
                                insertIndex = j;
                            }
                        }
                        //插入新的边界
                        Edge newEdge01 = new Edge();
                        Edge newEdge02 = new Edge();
                        newEdge01.Start = newEdges[insertIndex].Start;
                        newEdge01.End = i;
                        newEdge02.Start = i;
                        newEdge02.End = newEdges[insertIndex].End;
                        arrayEdges.RemoveAt(insertIndex);
                        arrayEdges.Insert(insertIndex, newEdge01);
                        arrayEdges.Insert(insertIndex + 1, newEdge02);
                        newMaxAngle = 0;
                    }
                }
            }
            //绘制新的边界
            double[] newLineCoords = new double[3 * (arrayEdges.Count + 1)];
            Edge[] lastEdges = new Edge[arrayEdges.Count];
            arrayEdges.CopyTo(lastEdges);
            for (i = 0; i < arrayEdges.Count; i++)
            {
                double[] p1 = new double[3], p2 = new double[3];
                p1[0] = arrPoints[3 * lastEdges[i].Start];
                p1[1] = arrPoints[3 * lastEdges[i].Start + 1];
                p1[2] = arrPoints[3 * lastEdges[i].Start + 2];
                p2[0] = arrPoints[3 * lastEdges[i].End];
                p2[1] = arrPoints[3 * lastEdges[i].End + 1];
                p2[2] = arrPoints[3 * lastEdges[i].End + 2];
                newLineCoords[3 * i] = arrPoints[3 * lastEdges[i].Start];
                newLineCoords[3 * i + 1] = arrPoints[3 * lastEdges[i].Start + 1];
                newLineCoords[3 * i + 2] = arrPoints[3 * lastEdges[i].Start + 2];
            }
            newLineCoords[3 * arrayEdges.Count - 3] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].Start];
            newLineCoords[3 * arrayEdges.Count - 2] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].Start + 1];
            newLineCoords[3 * arrayEdges.Count - 1] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].Start + 2];
            newLineCoords[3 * arrayEdges.Count] = arrPoints[3 * lastEdges[arrayEdges.Count - 1].End];
            newLineCoords[3 * arrayEdges.Count + 1] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].End + 1];
            newLineCoords[3 * arrayEdges.Count + 2] = arrPoints[3 * lastEdges[arrayEdges.Count -
           1].End + 2];
            AcadDoc.ModelSpace.Add3DPoly(newLineCoords);
        }
    }
}
